#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Contains the objects required for RASE port resource access. """

try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

import atom
import rawsc
from rawsc import RAWS_NAMESPACE
from rawsc.rase import *

class PortParams(rawsc.RawsParams):
    """ Handler for the <raws:params> element (= sub-element of atom:content). """
    _children = {}
    _children['{%s}id' % rawsc.RAWS_NAMESPACE] = ('id', Id)
    _children['{%s}nr_of_ports' % RAWS_NAMESPACE] = ('nr_of_ports', NrOfPorts)
    _children['{%s}description' % RAWS_NAMESPACE] = ('description', Description)
    _children['{%s}portnrs' % rawsc.RAWS_NAMESPACE] = ('portnrs', Portnrs)

    def __init__(self, id=None, nr_of_ports=None, description=None, portnrs=None, method=None, cdn=None, max_size=None, params=None):
        rawsc.RawsParams.__init__(self, extension_elements=None, extension_attributes=None)
        self.id = Id(text = id)
        self.nr_of_ports = NrOfPorts(text = nr_of_ports)
        self.description = Description(text = description)
        self.portnrs = Portnrs(text = portnrs)

def PortParamsFromString(xml_string):
    return atom.CreateClassFromXMLString(PortParams, xml_string)   


class PortContent(rawsc.RawsContent):
    """ Handler for the atom:content element, in a RAWS context. """
    _children = atom.AtomBase._children.copy()
    _children['{%s}params' % (RAWS_NAMESPACE)] = ('params', PortParams)
    
    params = None
    
    def __init__(self, params=None):
        rawsc.RawsContent.__init__(self, extension_elements=None, extension_attributes=None)
        self.params = PortParams()
        if params:
            self.params = params
    
def PortContentFromString(xml_string):
    return atom.CreateClassFromXMLString(PortContent, xml_string)   
    

class PortEntry(rawsc.RawsEntry):
    """ Handler for an entry that is returned by the RASE port resource."""
    _children = rawsc.RawsEntry._children.copy()
    _children['{%s}content' % atom.ATOM_NAMESPACE] = ('content', PortContent)

    def __init__(self, author=None, category=None, atom_id=None, link=None, published=None, title=None, updated=None, content = None, extension_elements=None, extension_attributes=None):
        rawsc.RawsEntry.__init__(self, author=author, category=category, atom_id=atom_id, link=link, published=published, title=title, updated=updated,
                                extension_elements=extension_elements, extension_attributes=extension_attributes)
        self.content = PortContent()
        if content:
            self.content = content

def PortEntryFromString(xml_string):
    return atom.CreateClassFromXMLString(PortEntry, xml_string)
    

class PortFeed(rawsc.RawsFeed, rawsc.LinkFinder):
    """ Handler for a feed that is returned by the RASE port resource."""
    _children = rawsc.RawsFeed._children.copy()
    _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [PortEntry])

def PortFeedFromString(xml_string):
  return atom.CreateClassFromXMLString(PortFeed, xml_string)

